#include "ebstate.h"
#include "QPainter"
#include "QStyleOption"

EbState::EbState(QWidget *parent) : QWidget(parent)
{
    initParam();

    connect(&mTimer, &QTimer::timeout, this, &EbState::refresh);

    mTimer.start(100);
}

void EbState::setInterval(quint16 interval)
{
    mTimer.setInterval(interval);
}

void EbState::setState(EB_STATE state)
{
    mState = state;
}

void EbState::refresh()
{
    this->update();
}

void EbState::paintEvent(QPaintEvent *event)
{
    QStyleOption tOpt;

    tOpt.initFrom(this);

    QPainter tPainter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &tOpt, &tPainter, this);
    QWidget::paintEvent(event);

    tPainter.setRenderHint(QPainter::Antialiasing);

    drawImage(&tPainter);
}

void EbState::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)

    initParam();
}

void EbState::initParam()
{
    QPoint tCenter;
    tCenter.setX(this->width() >> 1);
    tCenter.setY(this->height() >> 1);

    mEbRect.setLeft(tCenter.x() - (this->width() >> 1));
    mEbRect.setRight(tCenter.x() + (this->width() >> 1));
    mEbRect.setTop(tCenter.y() - (this->height() >> 1));
    mEbRect.setBottom(tCenter.y() + (this->height() >> 1));
}

void EbState::drawImage(QPainter *pPainter)
{
    QBrush tBrush(mEbColor);
    switch (mState) {
    case EB_STATE::ALARM:
        tBrush.setColor(mAlarmColor);
        pPainter->fillRect(mEbRect, tBrush);
        break;

    case EB_STATE::EB:
        pPainter->fillRect(mEbRect, tBrush);
        break;

    case EB_STATE::INIT:
    default:
        break;
    }
}
